package jianzhi_offer;

/**
 * @author: zhouyang
 * @descrpition:
 * @time: 2019-04-16 16:33
 */
public class _61_二叉搜索树的第k个结点 {

    public static void main(String[] args) {
        _61_二叉搜索树的第k个结点 test = new _61_二叉搜索树的第k个结点();
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(1);
        root.right = new TreeNode(4);
        Object obj = test.KthNode(root,3);
    }
    int index = 0;
    int k;
    TreeNode res = null;
    TreeNode KthNode(TreeNode pRoot, int k)
    {
        if(pRoot == null || k == 0) return null;
        this.k = k;
        help(pRoot);
        return this.res;
    }

    void help(TreeNode root){
        if(this.res != null) return ;
        if(root.left != null) help(root.left);
        ++index;
        if(this.k == this.index){
            this.res = root;
        }
        if(root.right != null) help(root.right);
    }
}
